package com.github.blanexie.tserver.http;

import lombok.extern.slf4j.Slf4j;

import java.util.Map;

@Slf4j
public class Range {
    public long start;
    public long end;
    public long length;
    public long total;

    /**
     * Construct a byte range.
     *
     * @param start Start of the byte range.
     * @param end   End of the byte range.
     * @param total Total length of the byte source.
     */
    public Range(long start, long end, long total) {
        this.start = start;
        this.end = end;
        this.length = end - start + 1;
        this.total = total;
    }

    public static long sublong(String value, int beginIndex, int endIndex) {
        String substring = value.substring(beginIndex, endIndex);
        return (substring.length() > 0) ? Long.parseLong(substring) : -1;
    }

    public boolean isFull() {
        return length == total;
    }

    public Map<String, String> getHeadMap() {
        return Map.of(
                "Content-Range", "bytes " + this.start + "-" + this.end + "/" + this.total,
                "Content-Length", length + ""
        );
    }


}
